package com.satan.novel.author.service.impl;

import com.satan.novel.author.dao.entity.AuthorInfo;
import com.satan.novel.author.dao.mapper.AuthorInfoMapper;
import com.satan.novel.author.dto.AuthorInfoDto;
import com.satan.novel.author.dto.req.AuthorRegisterReqDto;
import com.satan.novel.author.manager.cache.AuthorInfoCacheManager;
import com.satan.novel.author.manager.feign.BookFeignManager;
import com.satan.novel.author.service.AuthorService;
import com.satan.novel.book.dto.req.*;
import com.satan.novel.book.dto.resp.BookChapterRespDto;
import com.satan.novel.book.dto.resp.BookInfoRespDto;
import com.satan.novel.book.dto.resp.ChapterContentRespDto;
import com.satan.novel.common.auth.UserHolder;
import com.satan.novel.common.resp.PageRespDto;
import com.satan.novel.common.resp.RestResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @Author: Demon
 * @Date: 2024/2/3 15:38
 * @Description:
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class AuthorServiceImpl implements AuthorService {

    private final AuthorInfoCacheManager authorInfoCacheManager;
    private final AuthorInfoMapper authorInfoMapper;
    private final BookFeignManager bookFeignManager;

    @Override
    public RestResp<Integer> getStatus(Long userId) {
        AuthorInfoDto author = authorInfoCacheManager.getAuthor(userId);
        return Objects.isNull(author) ? RestResp.ok(null) : RestResp.ok(author.getStatus());
    }

    @Override
    public RestResp<Void> register(AuthorRegisterReqDto dto) {
        log.info("AuthorRegisterReqDto" + dto);
        dto.setUserId(UserHolder.getUserId());
        // 校验该用户是否已注册为作家
        AuthorInfoDto author = authorInfoCacheManager.getAuthor(dto.getUserId());
        if (Objects.nonNull(author)) {
            // 该用户已经是作家，直接返回
            return RestResp.ok();
        }
        // 保存作家注册信息
        AuthorInfo authorInfo = new AuthorInfo();
        authorInfo.setUserId(dto.getUserId());
        authorInfo.setChatAccount(dto.getChatAccount());
        authorInfo.setEmail(dto.getEmail());
        authorInfo.setInviteCode("0");
        authorInfo.setTelPhone(dto.getTelPhone());
        authorInfo.setPenName(dto.getPenName());
        authorInfo.setWorkDirection(dto.getWorkDirection());
        authorInfo.setCreateTime(LocalDateTime.now());
        authorInfo.setUpdateTime(LocalDateTime.now());
        authorInfoMapper.insert(authorInfo);
        // 清除作家缓存
        authorInfoCacheManager.evictAuthorCache();
        return RestResp.ok();
    }

    @Override
    public RestResp<PageRespDto<BookInfoRespDto>> listPublishBooks(BookPageReqDto dto) {
        return bookFeignManager.listPublishBooks(dto);
    }

    @Override
    public RestResp<Void> publishBook(BookAddReqDto dto) {
        return bookFeignManager.publishBook(dto);
    }

    @Override
    public RestResp<Void> publishBookChapter(ChapterAddReqDto dto) {
        return bookFeignManager.publishBookChapter(dto);
    }

    @Override
    public RestResp<PageRespDto<BookChapterRespDto>> listPublishBookChapters(ChapterPageReqDto chapterPageReqReqDto) {
        return bookFeignManager.listPublishBookChapters(chapterPageReqReqDto);
    }

    @Override
    public RestResp<Void> deleteBookChapter(Long chapterId) {
        return bookFeignManager.deleteBookChapter(chapterId);
    }

    @Override
    public RestResp<Void> updateBookChapter(Long chapterId, ChapterUpdateReqDto dto) {
        return bookFeignManager.updateBookChapter(chapterId, dto);
    }

    @Override
    public RestResp<ChapterContentRespDto> getBookChapter(Long chapterId) {
        return bookFeignManager.getBookChapter(chapterId);
    }
}
